package com.ruoyi.lxzx.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.lxzx.domain.UserCard;
import com.ruoyi.lxzx.mapper.UserCardMapper;
import com.ruoyi.system.service.ISysUserService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.lxzx.mapper.UserRealMapper;
import com.ruoyi.lxzx.domain.UserReal;
import com.ruoyi.lxzx.service.IUserRealService;

/**
 * realService业务层处理
 *
 * @author ruoyi
 * @date 2024-11-30
 */
@Service
@AllArgsConstructor
public class UserRealServiceImpl implements IUserRealService {
    private final UserRealMapper userRealMapper;
    private final UserCardMapper userCardMapper;
    private final ISysUserService userService;

    /**
     * 查询real
     *
     * @param id real主键
     * @return real
     */
    @Override
    public UserReal selectUserRealById(Long id) {
        return userRealMapper.selectUserRealById(id);
    }

    /**
     * 查询real列表
     *
     * @param userReal real
     * @return real
     */
    @Override
    public List<UserReal> selectUserRealList(UserReal userReal) {
        return userRealMapper.selectUserRealList(userReal);
    }

    /**
     * 新增real
     *
     * @param userReal real
     * @return 结果
     */
    @Override
    public int insertUserReal(UserReal userReal) {
        userReal.setCreateTime(DateUtils.getNowDate());
        return userRealMapper.insertUserReal(userReal);
    }

    /**
     * 修改real
     *
     * @param userReal real
     * @return 结果
     */
    @Override
    public int updateUserReal(UserReal userReal) {
        Long finalUserId = 0L;
        try {
            finalUserId = SecurityUtils.getUserId();
        } catch (Exception e) {
            throw new ServiceException("当前未登录,或登录已过期");
        }
        UserCard uc = new UserCard();
        uc.setUserId(userReal.getUserId());
        uc.setIsReal(userReal.getStatus());
        userCardMapper.updateUserCardRead(uc);
        SysUser su = new SysUser();
        su.setUserId(userReal.getUserId());
        su.setNickName(userReal.getName());
        userService.updateUseById(su);
        userReal.setAuditUser(finalUserId + "");
        userReal.setAuditTime(DateUtils.getNowDate());
        userReal.setUpdateBy(finalUserId + "");
        userReal.setUpdateTime(DateUtils.getNowDate());
        return userRealMapper.updateUserReal(userReal);
    }

    /**
     * 批量删除real
     *
     * @param ids 需要删除的real主键
     * @return 结果
     */
    @Override
    public int deleteUserRealByIds(Long[] ids) {
        return userRealMapper.deleteUserRealByIds(ids);
    }

    /**
     * 删除real信息
     *
     * @param id real主键
     * @return 结果
     */
    @Override
    public int deleteUserRealById(Long id) {
        return userRealMapper.deleteUserRealById(id);
    }

    @Override
    public UserReal selectUserRealByUserId(Long userId) {
        return userRealMapper.selectUserRealByUserId(userId);
    }
}
